package com.woniuxy.ptapp.app.jobhandler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.commons.util.DateUtil;
import com.woniuxy.commons.util.Result;
import com.woniuxy.ptapp.common.exception.EnumExceptionCode;
import com.woniuxy.ptapp.common.exception.WoniuxyException;
import com.woniuxy.ptapp.infra.model.ClassesOrder;
import com.woniuxy.ptapp.service.ClassesOrderService;
import com.woniuxy.relation.client.AnnouncementClient;
import com.woniuxy.relation.client.form.SendAnnouncementForm;
import com.woniuxy.userapp.client.FrontAccountClient;
import com.woniuxy.userapp.client.param.AutoDeductMoneyInfo;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Component
@Slf4j
public class UnpaidOvertimeHanlder {
    @Resource
    private FrontAccountClient frontAccountClient;
    @Resource
    private ClassesOrderService classesOrderService;
    @Resource
    private AnnouncementClient ac;

    @GlobalTransactional(rollbackFor = Exception.class)
    @XxlJob("UnpaidOvertime")
    public void demoJobHandler() {

        try {
            System.out.println("进入超时未支付执行器=================");
            //获取参数
            String jobParam = XxlJobHelper.getJobParam();
            String[] split = jobParam.split(",");
            //场馆账号id
            int venueAccountId = Integer.parseInt(split[0]);
            //订单金额
            BigDecimal autoMoney = new BigDecimal(split[1]);
            //订单id
            int classesOrderId = Integer.parseInt(split[3]);
            //学员账号id
            int studentAccountId = Integer.parseInt(split[2]);
            QueryWrapper<ClassesOrder> classesOrderQueryWrapper = new QueryWrapper<>();
            classesOrderQueryWrapper.eq("id", classesOrderId);
            ClassesOrder classesOrder = classesOrderService.getOne(classesOrderQueryWrapper);
            //判断订单是否已支付
            if (classesOrder.getState() != 1) {
                System.out.println("执行自动扣款==================");
                AutoDeductMoneyInfo autoDeductMoneyInfo = new AutoDeductMoneyInfo();
                autoDeductMoneyInfo.setMoneyAmount(autoMoney);
                autoDeductMoneyInfo.setDeductionId(studentAccountId);
                autoDeductMoneyInfo.setAddId(venueAccountId);
                Result result = frontAccountClient.autoDeductMoney(autoDeductMoneyInfo);
                if (result == null || result.getCode() != 200) {
                    throw new WoniuxyException(EnumExceptionCode.AutomaticDeductionsFailure.getCode(), EnumExceptionCode.AutomaticDeductionsFailure.getMessage());
                }
                classesOrder.setState(1);
                classesOrder.setPayTime(DateUtil.timeFormatString(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
                classesOrderService.saveOrUpdate(classesOrder);
                SendAnnouncementForm sendAnnouncementForm = new SendAnnouncementForm();
                sendAnnouncementForm.setAcceptAccountId(classesOrderId + "");
                sendAnnouncementForm.setContent("尊敬的会员" + classesOrder.getStudentName() + "由于您这笔订单" + classesOrder.getOrderNum() + "超时未支付,系统已经从您的钱包自动扣款,感谢您的支持理解,请知悉");
                Result result1 = ac.sendAnnouncement(sendAnnouncementForm);
            }
        } catch (NumberFormatException e) {
            System.out.println("出现异常,请工作人员及时处理================");
            throw e;
        } catch (WoniuxyException e) {
            System.out.println("出现异常,请工作人员及时处理================");
            throw e;
        }

    }
}
